Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | import { and, eq } from 'drizzle-orm' import { type NextRequest, NextResponse } from 'next/server' import { db, schema } from '@/db' import { getUserId } from '@/lib/viewer' import { withAuth } from '@/lib/auth/withAuth' /** * GET /api/teacher-flowcharts/[id] * Get a single teacher flowchart by ID * * Returns: { flowchart: TeacherFlowchart } or 404 */ export const GET = withAuth(async (_request, { params }) => { try { const { id } = (await params) as { id: string } const userId = await getUserId() const flowchart = await db.query.teacherFlowcharts.findFirst({ where: and(eq(schema.teacherFlowcharts.id, id), eq(schema.teacherFlowcharts.userId, userId)), }) if (!flowchart) { return NextResponse.json({ error: 'Flowchart not found' }, { status: 404 }) } return NextResponse.json({ flowchart }) } catch (error) { console.error('Failed to fetch teacher flowchart:', error) return NextResponse.json({ error: 'Failed to fetch flowchart' }, { status: 500 }) } }) /** * PUT /api/teacher-flowcharts/[id] * Update a teacher flowchart * * If the flowchart is published, this creates a new version instead of updating in place. * * Body: { * title?: string * description?: string * emoji?: string * difficulty?: 'Beginner' | 'Intermediate' | 'Advanced' * definitionJson?: string * mermaidContent?: string * searchKeywords?: string * } * * Returns: { flowchart: TeacherFlowchart } */ export const PUT = withAuth(async (request, { params }) => { try { const { id } = (await params) as { id: string } const userId = await getUserId() const body = await request.json() // Find existing flowchart const existing = await db.query.teacherFlowcharts.findFirst({ where: and(eq(schema.teacherFlowcharts.id, id), eq(schema.teacherFlowcharts.userId, userId)), }) if (!existing) { return NextResponse.json({ error: 'Flowchart not found' }, { status: 404 }) } // Validate definition JSON if provided if (body.definitionJson) { try { JSON.parse(body.definitionJson) } catch { return NextResponse.json({ error: 'Invalid definition JSON' }, { status: 400 }) } } // Validate difficulty if provided const validDifficulties = ['Beginner', 'Intermediate', 'Advanced'] if (body.difficulty && !validDifficulties.includes(body.difficulty)) { return NextResponse.json({ error: 'Invalid difficulty level' }, { status: 400 }) } const now = new Date() // If published, create a new version if (existing.status === 'published') { const [newVersion] = await db .insert(schema.teacherFlowcharts) .values({ userId, title: body.title ?? existing.title, description: body.description ?? existing.description, emoji: body.emoji ?? existing.emoji, difficulty: body.difficulty ?? existing.difficulty, definitionJson: body.definitionJson ?? existing.definitionJson, mermaidContent: body.mermaidContent ?? existing.mermaidContent, searchKeywords: body.searchKeywords ?? existing.searchKeywords, version: existing.version + 1, parentVersionId: existing.id, status: 'draft', // New version starts as draft createdAt: now, updatedAt: now, }) .returning() return NextResponse.json({ flowchart: newVersion, isNewVersion: true }) } // Otherwise, update in place const updates: Partial<typeof existing> = { updatedAt: now } if (body.title !== undefined) updates.title = body.title if (body.description !== undefined) updates.description = body.description if (body.emoji !== undefined) updates.emoji = body.emoji if (body.difficulty !== undefined) updates.difficulty = body.difficulty if (body.definitionJson !== undefined) updates.definitionJson = body.definitionJson if (body.mermaidContent !== undefined) updates.mermaidContent = body.mermaidContent if (body.searchKeywords !== undefined) updates.searchKeywords = body.searchKeywords const [flowchart] = await db .update(schema.teacherFlowcharts) .set(updates) .where(eq(schema.teacherFlowcharts.id, id)) .returning() return NextResponse.json({ flowchart }) } catch (error) { console.error('Failed to update teacher flowchart:', error) return NextResponse.json({ error: 'Failed to update flowchart' }, { status: 500 }) } }) /** * DELETE /api/teacher-flowcharts/[id] * Archive a teacher flowchart (soft delete) * * Returns: { success: true } */ export const DELETE = withAuth(async (_request, { params }) => { try { const { id } = (await params) as { id: string } const userId = await getUserId() // Verify ownership const existing = await db.query.teacherFlowcharts.findFirst({ where: and(eq(schema.teacherFlowcharts.id, id), eq(schema.teacherFlowcharts.userId, userId)), }) if (!existing) { return NextResponse.json({ error: 'Flowchart not found' }, { status: 404 }) } // Soft delete by setting status to archived await db .update(schema.teacherFlowcharts) .set({ status: 'archived', updatedAt: new Date(), }) .where(eq(schema.teacherFlowcharts.id, id)) return NextResponse.json({ success: true }) } catch (error) { console.error('Failed to delete teacher flowchart:', error) return NextResponse.json({ error: 'Failed to delete flowchart' }, { status: 500 }) } }) |